package com.red.wood.common.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


public class CommonUtils {
	
	public static String valueOf(Object object){
		String str = StringUtils.trim(String.valueOf(object));
		boolean check = StringUtils.isBlank(str) || StringUtils.equals(str, "null") || StringUtils.equals(str, "undefined");
		return check ? "" : str;
	}
	
	public static String filteNull(Object object) {
		String str = valueOf(object);
		boolean check = StringUtils.isBlank(str);
		return check ? "0" : str;
	}
	
	/**
	 * 流量单位自适应
	 * @param data原始单位B
	 * @return
	 */
	public static String formateFlow(double data){
		java.text.DecimalFormat df = new java.text.DecimalFormat("#.###");
		int count = 0;
	    String _data = "";
	    
	    while(data >= 1024){
	    	data = data / 1024;
	    	count ++;
	    }
	    data = Double.parseDouble(df.format(data));
		switch (count) {
			case 0:
				_data = data + "B";
				break;
			case 1:
				_data = data + "KB";
				break;
			case 2:
				_data = data + "MB";
				break;
			case 3:
				_data = data + "GB";
				break;
			case 4:
				_data = data + "TB";
				break;
			case 5:
				_data = data + "PB";
				break;
			case 6:
				_data = data + "EB";
				break;
			case 7:
				_data = data + "ZB";
				break;
		}
		return _data;
	}
	
	public static String getLoginUserProvince(String loginUserName) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("gdtxglj","广东");
		map.put("bjtxglj","北京");
		map.put("sdtxglj","山东");
		map.put("sctxglj","四川");
		map.put("tjtxglj","天津");
		map.put("cqtxglj","重庆");
		map.put("fjtxglj","福建");
		map.put("jstxglj","江苏");
		map.put("hbtxglj","湖北");
		map.put("shtxglj","上海");
		map.put("yntxglj","云南");
		map.put("hntxglj","湖南");
		map.put("sxtxglj","陕西");
		map.put("gxtxglj","广西");
		map.put("zjtxglj","浙江");
		map.put("ahtxglj","安徽");
		map.put("hebeitxglj","河北");
		map.put("henantxglj","河南");
		map.put("jxtxglj","江西");
		map.put("lntxglj","辽宁");
		map.put("xjtxglj","新疆");
		map.put("gstxglj","甘肃");
		map.put("hljtxglj","黑龙江");
		map.put("jntxglj","吉林");
		map.put("hainantxglj","海南");
		map.put("shanxitxglj","山西");
		map.put("gztxglj","贵州");
		map.put("nxtxglj","宁夏");
		map.put("qhtxglj","青海");
		map.put("nmgtxglj","内蒙古");
		map.put("xztxglj","西藏");
		return map.get(loginUserName);
	}
	
	public static String getProvinceId(String name) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("台湾","710000");
		map.put("河南","410000");
		map.put("江西","360000");
		map.put("吉林","220000");
		map.put("陕西","610000");
		map.put("山西","140000");
		map.put("上海","310000");
		map.put("江苏","320000");
		map.put("浙江","330000");
		map.put("黑龙江","230000");
		map.put("四川","510000");
		map.put("山东","370000");
		map.put("湖北","420000");
		map.put("内蒙古","150000");
		map.put("辽宁","210000");
		map.put("湖南","430000");
		map.put("海南","460000");
		map.put("重庆","500000");
		map.put("广西","450000");
		map.put("福建","350000");
		map.put("贵州","520000");
		map.put("云南","530000");
		map.put("青海","630000");
		map.put("香港","810000");
		map.put("澳门","820000");
		map.put("安徽","340000");
		map.put("北京","110000");
		map.put("天津","120000");
		map.put("河北","130000");
		map.put("广东","440000");
		map.put("西藏","540000");
		map.put("甘肃","620000");
		map.put("宁夏","640000");
		map.put("新疆","650000");
		String provinceId = map.get(name);
		return StringUtils.isNotBlank(provinceId) ? provinceId : "";
	}
	/**
	 * 数字统计单位自适应
	 * @param data原始单位个
	 * @return
	 */
	public static String[] formateNum(double data){
	    java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
		int count = 0;
	    //String _data = "";
	    while(data >= 10000){
	    	data = data / 10000;
	    	count ++;
	    }
	    String formatdata = df.format(data);
	    String[] str = new String[2];
	    str[0] = formatdata;
	    switch (count) {
			case 0:
				//_data = data + "个";
				str[1] = "个";
				break;
			case 1:
				//_data = data + "万";
				str[1] = "万";
				break;
			case 2:
				//_data = data + "亿";
				str[1] = "亿";
				break;
		}
		return str;
	}
	
	/**
	 * 特殊字符校验，防扫描
	 * @param list
	 */
	public static void fieldCheck(List<String[]> list){
		for(String[] str : list){
			if(str.length < 2){
				continue;
			}
			if(str[0].indexOf("'") >= 0 || str[0].indexOf("\"") >= 0){
				throw new RuntimeException("【"+str[1]+"】不能包含单引号和双引号！");
			}
		}
	}
	
	/**
	 * @Description：获取临时路径，excel导出、文件上传、下载路径
	 * @return
	 */
	public static String getDataTempPath(){
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String path=request.getSession().getServletContext().getRealPath("/data/temp")+File.separator;
		if (StringUtils.isNotEmpty(path)) {
	        File file = new File(path);
	        if (!file.exists()) {
	            file.mkdirs();
	        }
		}
		return path;
	}
	
	/**
	 * 获取导入模板
	 * @return
	 */
	public static String getExportTemplate(){
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		String path=request.getSession().getServletContext().getRealPath("/excel_template")+File.separator;
		if (StringUtils.isNotEmpty(path)) {
	        File file = new File(path);
	        if (!file.exists()) {
	            file.mkdirs();
	        }
		}
		return path;
	}
	
	/**
	 * IP校验
	 * @param ip
	 * @return
	 */
	public static boolean isValidIp(String ip){
		String reg = "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\."
				+ "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\."
				+ "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\."
				+ "(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$";
		Pattern pattern = Pattern.compile(reg);
		Matcher p = pattern.matcher(ip);
		return p.matches();
	}
	
	public static boolean isValidDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			sdf.setLenient(false);//严格按照给定的格式来校验
			sdf.parse(date);
			return true;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	/**
	 * 转换为Integer类型
	 */
	public static Integer toInteger(Object val){
		return toLong(val).intValue();
	}
	
	/**
	 * 转换为Long类型
	 * @param val
	 * @return
	 */
	public static Long toLong(Object val){
		return toDouble(val).longValue();
	}
	
	/**
	 * 转换为Double类型
	 * @param val
	 * @return
	 */
	public static Double toDouble(Object val){
		if (val == null){
			return -999D;
		}
		try {
			return Double.valueOf(valueOf(val));
		} catch (Exception e) {
			return -999D;
		}
	}
	
	public static byte[] toHexByte(byte[] b){
    	byte[] hexByte=new byte[b.length];
        for (int i = 0; i < b.length; ++i){
        	hexByte[i]=(byte)(b[i]& 0xFF);
        }
        return hexByte;
    }
    public static String toHexString(byte[] b){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < b.length; i++){
            buffer.append(toHexString(b[i]));
        }
        return buffer.toString();
    }
    public static String toHexString(byte b){
        String s = Integer.toHexString(b & 0xFF);
        if (s.length() == 1){
            return "0" + s;
        }else{
            return s;
        }
    }
}
